package com.raja.servingStation.common.redis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class MGDecoder {
	public static final byte MP_SPEED = 0x01;

	public static final byte MP_GPS = 0x02;

	public static final byte MP_CAN = 0x03;

	public static final byte MP_EXTEND = 0x04;

	public static final byte MP_AD = 0x05;

	public static final byte MP_INVAILD = 0x00;

	public static final byte MP_SWITCH_VALUE = 0x0A;

	protected static final int MAX_SIZE = 1000;

	private static final ThreadLocal<SimpleDateFormat> LOG_DATE_FORMAT = DateFormatUtil
			.threadLocalDateFormat("yyyyMMddHHmmss");

	public static long tick2time_t(long tick) {
		try {
			Date date = LOG_DATE_FORMAT.get().parse(String.valueOf(tick));
			return date.getTime() / 1000;
		} catch (ParseException e) {
		}
		return 0;
	}

	public static long time_t2tick(long time_t) {
		Date date = new Date(time_t * 1000);
		String str = LOG_DATE_FORMAT.get().format(date);
		return Long.parseLong(str);
	}
	//	little-endian
	public static int byte1ToLong(byte bt) {
		int n = 0;
		n = bt;
		if (bt < 0)
			n += 256;
		return n;
	}

	//	little-endian
	public static short byte1ToShort(byte bt) {
		short n = 0;
		n = bt;
		if (bt < 0) {
			n += 256;
		}
		return n;
	}

	//	little-endian
	public static int byte2ToLong(byte[] buf, int start) {
		int n = 0;
		if (null != buf) {
			for (int i = 1; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x00ff);
			}
		}
		return n;
	}

	//	little-endian
	public static int byte2ToInt(byte[] buf, int start) {
		int n = 0;
		if (null != buf) {
			for (int i = 1; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x00ff);
			}
		}
		return n;
	}

	// little-endian
	public static long byte4ToLong(byte[] buf, int start) {
		long n = 0;
		if (null != buf) {
			for (int i = 3; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x000000ff);
			}
		}
		return n;
	}
	// little-endian
	public static int byte4ToInt(byte[] buf, int start) {
		int n = 0;
		if (null != buf) {
			for (int i = 3; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x000000ff);
			}
		}
		return n;
	}

	// little-endian
	public static long byte8ToLong(byte[] buf, int start) {
		long n = 0l;
		if (null != buf) {
			for (int i = 7; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x000000ff);
			}
		}
		return n;
	}

	//	big-endian
	public static int byte1ToLongByBE(byte bt) {
		int n = 0;
		n = bt;
		if (bt < 0)
			n += 256;
		return n;
	}
	//	big-endian
	public static int byte2ToLongByBE(byte[] buf, int start) {
		int n = 0;
		if (null != buf) {
			for (int i = 0; i <= 1; i++) {
				n <<= 8;
				n |= (buf[start + i] & 0x00ff);
			}
		}
		return n;
	}

	// big-endian
	public static long byte4ToLongByBE(byte[] buf, int start) {
		long n = 0;
		if (null != buf) {
			for (int i = 0; i <= 3; i++) {
				n <<= 8;
				n |= (buf[start + i] & 0x000000ff);
			}
		}
		return n;
	}

	// big-endian
	public static long byte8ToLongByBE(byte[] buf, int start) {
		long n = 0l;
		if (null != buf) {
			for (int i = 0; i <= 7; i++) {
				n <<= 8;
				n |= (buf[start + i] & 0x000000ff);
			}
		}
		return n;
	}

	public static byte[] shortToByte(int n) {
		byte[] buf = new byte[2];

		buf[0] = (byte) (n & 0x000000FF);
		buf[1] = (byte) ((n & 0x0000FF00) >> 8);

		return buf;
	}

	public static byte[] intToByte(int n) {
		byte[] buf = new byte[4];

		buf[0] = (byte) (n & 0x000000FF);
		buf[1] = (byte) ((n & 0x0000FF00) >> 8);
		buf[2] = (byte) ((n & 0x00FF0000) >> 16);
		buf[3] = (byte) ((n & 0xFF000000) >> 24);

		return buf;
	}

	public static byte[] intToByte(long n) {
		byte[] buf = new byte[4];

		buf[0] = (byte) (n & 0x000000FF);
		buf[1] = (byte) ((n & 0x0000FF00) >> 8);
		buf[2] = (byte) ((n & 0x00FF0000) >> 16);
		buf[3] = (byte) ((n & 0xFF000000) >> 24);

		return buf;
	}

	public static byte[] longToByte(long n)
	{
		byte[] buf = new byte[4];

		buf[0] = (byte)( n & 0x000000FF );
		buf[1] = (byte)((n & 0x0000FF00 )>> 8);
		buf[2] = (byte)((n & 0x00FF0000 )>> 16);
		buf[3] = (byte)((n & 0xFF000000 )>> 24);

		return buf;
	}

	public static short shortToInt(byte[] b, int index) {
	      return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));
	}

	/**
	 *
	 * @param num 要获取二进制值的数
	 * @param index 倒数第一位为0 依次类推
	 * @return
	 */
	public static int get(int num,int index){
		return (num & (0x1 << index))>> index;
	}
}